<template>
  <div>
    <el-card class="box-card">
      <el-button style="float: right;" type="primary" @click="Permissions=true,addPermission(1, '0')">添加权限</el-button>
      <el-table
        :default-sort="{prop: 'date', order: 'descending'}"

        :data="PermiostableData"
        row-key="id"
        border
        style="width: 100%"
      >
        <el-table-column
          fixed
          type="index"
          prop="date"
          label="序号"
          width="200"
        />
        <el-table-column
          prop="name"
          label="姓名"
          width="300"
        />

        <el-table-column
          prop="code"
          label="标识"
          width="300"
        />
        <el-table-column
          prop="description"
          label="描述"
          width="300"
        />

        <el-table-column

          label="操作"
          width="300"
        >
          <template slot-scope="scope">
            <el-button v-if="scope.row.type===1" type="text" size="small" @click="Permissions=true,addPermission(2, scope.row.id)">添加</el-button>
            <el-button type="text" size="small" @click="distPermissionFn(scope.row.id)">编辑</el-button>
            <el-button type="text" size="small" @click="removePermissionFn(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 单床 -->

      <!-- 放置一个弹层 用来编辑新增节点 -->
      <el-dialog :title="isList===false? '新增权限点':'编辑权限' " :visible="Permissions" @close="btnCancel">
        <!-- 表单 -->
        <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="submitFn('formData')">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>

    </el-card>
  </div>
</template>

<script>

import { GetEmployeePermissionsAPI, GetuserPermissionsAPI, UpdatePermissionsAPI } from '@/api/PermissionAPI'
import { TreeData } from '@/utils/shuxin'
import { AddPermissionsAPI, delPermissionsAPI } from '@/api/PermissionAPI'

export default {
  name: 'Permission',
  data() {
    // 验证部门名称
    const NamevalidatePass = (to, value, baloack) => {
      this.shuList.filter(itme => itme.pid === this.formData.pid).map(itme => itme.name)
      let Myname = this.shuList.filter(itme => itme.pid === this.formData.pid).map(itme => itme.name)
      if (this.isList) {
        Myname = this.shuList.filter(itme => itme.id !== this.formData.id).map(itme => itme.name)
      }
      Myname.includes(value) ? baloack(new Error(`${value}已存在`)) : baloack()
    }
    // 验证编码
    const CodevalidatePass = (to, value, baloack) => {
      let Mycode = this.shuList.filter(itme => itme.code === value).map(itme => itme.code)
      if (this.isList) {
        Mycode = this.shuList.filter(itme => itme.id !== this.formData._id).map(itme => itme.code)
      }
      Mycode.includes(value) ? baloack(new Error(`${value}已存在`)) : baloack()
    }
    return {
      isList: false, // 编辑/新增状态
      PermiostableData: [], // 数据
      shuList: [],
      Permissions: false,
      showDialog: false, // 是否显示弹层
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' },
          { validator: NamevalidatePass, trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' },
          { validator: CodevalidatePass, trigger: 'blur' }]
      }
    }
  },
  created() {
    this.GetEmployeePermissions()
  },
  methods: {
    async GetEmployeePermissions() {
      const res = await GetEmployeePermissionsAPI()
      console.log(res)
      this.PermiostableData = TreeData(res.data, '0')
      this.shuList = res.data.map(({ id, name, pid, code }) => ({
        id,
        name: name.trim(),
        pid,
        code
      }))
      console.log(this.shuList)
    },
    // 点击添加
    async addPermission(type, id) {
      // 记录当前添加的关键信息
      this.formData.type = type
      this.formData.pid = id

      this.isList = false
    },
    // 点击编辑
    async distPermissionFn(id) {
      this.Permissions = true

      console.log(id)

      this.isList = true // 编辑新增状态
      const res = await GetuserPermissionsAPI(id)
      console.log(res)
      this.formData = res.data
    },
    // 点击删除
    async removePermissionFn(id) {
      const res = await delPermissionsAPI(id)
      console.log(res)
      this.GetEmployeePermissions()
    },
    // 取消
    btnCancel() {
      this.$refs.perForm.resetFields()
      this.Permissions = false
    },
    // Sub确定
    submitFn() {
      this.$refs.perForm.validate(async(valid) => {
        if (!valid) return

        !this.isList ? this.AddPermissions() : this.UpdatePermissions()
      })
    },
    // 更新方法
    async   UpdatePermissions() {
      const res = await UpdatePermissionsAPI(this.formData)
      console.log(res)
      this.Permissions = false
      this.$refs.perForm.resetFields()
      this.GetEmployeePermissions()
    },
    // 新增方法
    async AddPermissions() {
      const res = await AddPermissionsAPI(this.formData)
      console.log(res)
      this.$message.success('权限添加成功')
      this.$refs.perForm.resetFields()
      this.Permissions = false
      this.GetEmployeePermissions()
    }

  }
}
</script>

<style>
.app-main{
  padding: 40px;
}
</style>
